package com.common.loginInterceptor;


import com.ssh.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 文 件 名 称：Login.
 * 文 件 说 明：
 * 创   建   人：zpen.
 * 创 建 时 间：2018/3/5 16:16.
 */
public class Login implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String url = request.getRequestURI();
        String ctx = request.getContextPath();
        if (request.getAttribute("ctx") == null) {
            request.setAttribute("ctx", ctx);
        }
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if((ctx+"/login.action").equals(url)){
            return true;
        }
        if (user!=null){
            return true;
        }
//        request.getRequestDispatcher("login").forward(request,response);
        response.sendRedirect(ctx+"/login.action");
        return false;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
